package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserMessage;
import com.qqtech.qquan.user.model.UserMessageQuery;
import com.qqtech.qquan.user.model.UserMessageVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserMessageService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/message")
public class UserMessageController extends BaseWebController<UserMessage, UserMessageQuery> {
	@Resource
	private UserMessageService userMessageService;
	@Resource
	private UserBaseService userBaseService;
    
	@Override
	protected BaseService<UserMessage> getBaseService() {
		return userMessageService;
	}
	
	
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserMessage message) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		message.setFromId(curUserId);
		userMessageService.addMessage(message);
		return result;
	}
	
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/myMessageFromHer")
	@ResponseBody
	public Result myMessageFromHer(@RequestBody UserMessageQuery messageQ){
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer herUserId = messageQ.getFromId();
		UserBaseVo userVo= userBaseService.getById(herUserId);//用于取用户呢称、头像
		if(herUserId==null){
			result.addErr("留言者id为空！");
			return result;
		}
		messageQ.setToId(curUserId);
		
		Pageable pageKit = new PageKit(messageQ.getPage(), new PageOrder(Direction.DESC, "newAt"));
		List<UserMessageVo> messageList = userMessageService.queryList(messageQ, pageKit);
			if(messageList!=null && messageList.size()>0){
				for(UserMessageVo messageVo:messageList){
					messageVo.setFromLogo(StringUtil.isNotBlank(userVo.getLogo())?userVo.getLogo():"source://image/common/userLogin.png");
					messageVo.setFromName(StringUtil.isNotBlank(userVo.getNick())?userVo.getNick():"新手");
				}
			}
		
		result.put("messageList", messageList);
		return result;
	}
}
